package com.stock.project.system.stockorders.service;

import com.stock.common.support.Convert;
import com.stock.common.utils.GeneratorUtils;
import com.stock.project.system.inventory.domain.Inventory;
import com.stock.project.system.inventory.mapper.InventoryMapper;
import com.stock.project.system.stockorders.domain.StockOrders;
import com.stock.project.system.stockorders.mapper.StockOrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 库存订单 服务层实现
 *
 * @author stock
 * @date 2019-03-14
 */
@Service
public class StockOrdersServiceImpl implements IStockOrdersService {

    @Autowired
    private StockOrdersMapper stockOrdersMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 查询库存订单信息
     *
     * @param id 库存订单ID
     * @return 库存订单信息
     */
    @Override
    public StockOrders selectStockOrdersById(Integer id) {
        return stockOrdersMapper.selectStockOrdersById(id);
    }

    /**
     * 查询库存订单列表
     *
     * @param stockOrders 库存订单信息
     * @return 库存订单集合
     */
    @Override
    public List<StockOrders> selectStockOrdersList(StockOrders stockOrders) {
        return stockOrdersMapper.selectStockOrdersList(stockOrders);
    }

    /**
     * 新增库存订单
     *
     * @param stockOrders 库存订单信息
     * @return 结果
     */
    @Override
    public int insertStockOrders(StockOrders stockOrders) {
        Inventory inventory = inventoryMapper.selectInventoryByCode(stockOrders.getGoodsCode(),stockOrders.getWarehouseCode());
        if(inventory ==null){
            if(1 == stockOrders.getType()){
                inventory = new Inventory();
                inventory.setGoodsCode(stockOrders.getGoodsCode());
                inventory.setGoodsName(stockOrders.getGoodsName());
                inventory.setWarehouseCode(stockOrders.getWarehouseCode());
                inventory.setQuantity(stockOrders.getQuantity());
                inventory.setFreezingInventory(0);
                inventory.setOperator("sys");
                inventory.setCreateTime(new Date());
                inventory.setUpdateTime(new Date());
                inventory.setRemark("销售入库单创建库存");
                inventoryMapper.insertInventory(inventory);
            }else {
                //销售出库单商品不存在
                return 0;
            }
        }else {
            if(1 == stockOrders.getType()) {
                inventory.setQuantity(inventory.getQuantity() + stockOrders.getQuantity());
                inventoryMapper.updateInventory(inventory);
            }else {
                if(inventory.getQuantity() == 0 || inventory.getQuantity() < stockOrders.getQuantity()){
                    //销售出库单库存不足
                    return -1;
                }
                inventory.setQuantity(inventory.getQuantity() - stockOrders.getQuantity());
                inventoryMapper.updateInventory(inventory);
            }
        }
        stockOrders.setOrderId(GeneratorUtils.generatorOrderNumber());
        stockOrders.setCreateTime(new Date());
        stockOrders.setUpdateTime(new Date());
        return stockOrdersMapper.insertStockOrders(stockOrders);
    }

    /**
     * 修改库存订单
     *
     * @param stockOrders 库存订单信息
     * @return 结果
     */
    @Override
    public int updateStockOrders(StockOrders stockOrders) {
        stockOrders.setUpdateTime(new Date());
        return stockOrdersMapper.updateStockOrders(stockOrders);
    }

    /**
     * 删除库存订单对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStockOrdersByIds(String ids) {
        return stockOrdersMapper.deleteStockOrdersByIds(Convert.toStrArray(ids));
    }

}
